/**
 * 
 */
package com.aspire.android.heliar.raw.mms.tools;

import java.io.File;
import java.io.IOException;
import java.util.List;

import android.os.Environment;
import android.util.Log;

import com.aspire.android.heliar.Heliar;
import com.aspire.android.heliar.raw.mms.MMS;
import com.aspire.android.heliar.raw.mms.MMSManager;
import com.aspire.android.heliar.utils.FileSystem;

/**
 * @author panglei
 * 
 */
public class MMSExporter {

	public static final String MMS_EXPORT_CHARSET = "UTF-8";

	public static void export(List<MMS> mmss) {
		for (MMS mms : mmss) {
			String filename = mms.getSubject() + "_" + mms.getAddress() + "_"
					+ ".txt";
			
			mms = MMSManager.getInstance().getMMS(mms.getId());
			export(mms, filename);
		}
	}

	public static void export(MMS mms, String filename) {
		String content = mms.getFullContent();

		Log.d(Heliar.LOG_TAG, content);

		if (FileSystem.isExternalStorageWriteable()) {
			File sdCard = Environment.getExternalStorageDirectory();
			File file = new File(sdCard + "/mb", filename);
			try {
				FileSystem.writeFile(content, file, MMS_EXPORT_CHARSET, true);
			} catch (IOException e) {
				Log.v(Heliar.LOG_TAG, e.getMessage());
			}
		}
	}
}
